package service.Impl;

import dao.*;
import entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pojo.jinhuo;
import service.OkgodownDetailsDao;
import service.RepertorySDao;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class OkgodownDetailsDaoDaoImpl implements OkgodownDetailsDao {
    @Autowired
    private godownDetailsDao godownDetailsDao;
    @Autowired
    private RepertoryDao repertoryDao;
    @Autowired
    public InoutDao inoutDao;
    @Autowired
    public ArauditorDetailsDao arauditorDetailsDao;
    @Autowired
    public RepertorySDao repertorySDao;
    @Autowired
    public godownDao godownDao;
    @Autowired
    public firmDao firmDao;
    @Autowired
    public collectDao collectDao;
    @Autowired
    public detailsDao detailsDao;

    @Override
    public void shenhe(String seat, String remark,HttpServletRequest request) {

        //取出正在登录的管理员所有信息
        Administrator administrator=(Administrator) request.getSession().getAttribute("ad");
        List<Godowndetails> list = godownDetailsDao.showGos(seat);
        for (Godowndetails godowndetails : list) {
            Repertory repertory = new Repertory();
            repertory.setIptypeid(godowndetails.getShop().getIptypeid());
            repertory.setFirmId(godowndetails.getFirm().getFirmId());
            repertory.setModel(godowndetails.getShop().getShopType());
            repertory.setRepertoryName(godowndetails.getShop().getShopName());
            repertory.setRepertoryColor(godowndetails.getShop().getShopcolor());
            Repertory repertory1 = repertoryDao.flagrepertory(repertory);
            if (repertory1 != null) {
                if (godowndetails.getShop().getShopprice() != repertory1.getPrice()) {//本次价格和上次价格不同将先修改价格
                    repertorySDao.upkucun(repertory1.getRepertoryId(),godowndetails.getShop().getShopprice());
                }
                repertoryDao.upkucunNum(repertory1.getRepertoryId(),godowndetails.getGdNums());//增加库存数量
                repertoryDao.upjiechu(repertory1.getRepertoryId());//解除库存报警
            }else{
                Repertory repertory2=new Repertory();
                repertory2.setRepertoryColor(godowndetails.getShop().getShopcolor());
                repertory2.setFirmId(godowndetails.getFirm().getFirmId());
                repertory2.setRepertoryName(godowndetails.getShop().getShopName());
                repertory2.setIptypeid(godowndetails.getShop().getIptypeid());
                repertory2.setModel(godowndetails.getShop().getShopType());
                repertory2.setPrice(godowndetails.getShop().getShopprice());
                repertory2.setPresetPrice(godowndetails.getShop().getShopprice()*0.2+godowndetails.getShop().getShopprice());
                repertory2.setTotal(godowndetails.getGdNums()*repertory2.getPresetPrice());
                repertory2.setRepertoryRemark(godowndetails.getBeizhu());
                repertory2.setRepertoryNum(godowndetails.getGdNums());
                repertoryDao.addkucun(repertory2);//添加新库存信息
            }

        }
        godownDao.shenheSeat(seat,administrator.getAname(),remark);//审核信息
        List<Firm> list1=firmDao.showFirm();
        for(Firm firm:list1){
            jinhuo jinhuo=godownDetailsDao.showfirmTotal(seat,firm.getFirmId());
            Collect collect=collectDao.flagCollects(firm.getFirmName());
            if(jinhuo==null){
                break;
            }
            if(collect!=null){
                collectDao.upfirmToltal(collect.getCollectid(),jinhuo.getTotal());
            }else{
                Collect collect1=new Collect();
                collect1.setMyprice(0);
                collect1.setFlatType("供应商");
                collect1.setBtype(firm.getFirmName());
                collect1.setHeprice(jinhuo.getTotal());
                collectDao.addCollect(collect1);

            }
            Details details=new Details();
            details.setSeat(seat);
            details.setSynopsis("商品进货");
            details.setBtype(firm.getFirmName());
            details.setIphoneNum(firm.getFirmIphone());
            details.setAmount(-(jinhuo.getTotal()));
            details.setPaid(-(jinhuo.getTotal()));
            details.setAid(administrator.getAid());
            detailsDao.addDetails(details);
        }



    }
}
